package com.sharemarking.application.wechat.controller;

import com.aliyun.oss.OSS;
import com.sharemarking.application.common.entity.File;
import com.sharemarking.application.common.repository.FileRepo;
import com.sharemarking.application.common.utils.OSSPutObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by cjw on 2017/11/25.
 */
@Controller
@RequestMapping("fileHandle")
public class FileHandleController {
    //@Value("${oss.endpoint}")
    private String endpoint = "testoss0.oss-cn-qingdao.aliyuncs.com/";

    @Autowired
    FileRepo fileRepo;

    /**
     * 上传图片
     * @param file
     * @param md5
     * @throws IOException
     */
    @RequestMapping(value="uploadFile",method= RequestMethod.POST)
    @Transactional
    @ResponseBody
    public Map<String,Object> uploadFile(MultipartFile file, String md5)throws Exception{
        System.out.println("=====uploadFile");
        Map<String,Object> map=new HashMap<String,Object>();
        String fileName=System.currentTimeMillis()+file.getOriginalFilename();
        OSSPutObject.putFileInputStream(file.getInputStream(),fileName);
        File newFile = new File();
        newFile.setFileUrl(endpoint+fileName);
        newFile.setFileName(fileName);
        newFile.setFileSize(file.getSize());
        newFile.setType(0);
        newFile.setDelFlag(true);
        fileRepo.save(newFile);
        map.put("fileId", newFile.getId());
        return map;
    }
}
